﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ListenMusic.Models.ViewModel;
using System.ServiceModel;
using WMPLib;
using System.IO;
namespace ListenMusic.Models.Reposistory.Public
{
    
    public class ListenReposistory
    {
        SongReposistory _song;
        AlbumReposistory _album;
        MusicianReposistory _musician;
        CategoryReposistory _category;
        VideoReposistory _video;
        CommentReposistory _comment;
        public ListenReposistory()
        {
            _song = new SongReposistory();
            _album = new AlbumReposistory();
            _musician = new MusicianReposistory();
            _category = new CategoryReposistory();
            _video = new VideoReposistory();
            _comment = new CommentReposistory();
        }
        public ListenPage loadListenPage(int start, int end,int songid)
        {            
            try
            {
                var q = from s in _song.getAllSonginAlbum()
                        join a in _album.getAllAlbum() on s.AlbumID equals a.AlbumID
                        join m in _musician.getAllMusician() on s.MusicianID equals m.MusicianID
                        join c in _category.getAllCategory() on s.CategoryID equals c.CategoryID
                        where s.SongID.Equals(songid)
                        select new ListenPage()
                        {
                            SongID = songid,
                            SingerID = s.SingerID,
                            SingerName = s.SingerName,
                            SongName = s.SongName,
                            SongURL = s.SongURL,
                            Lyrics = s.Lyrics,
                            AlbumID = a.AlbumID,                            
                            AlbumName = a.AlbumName,
                            MusicianID = m.MusicianID,
                            MusicianName = m.MusicianName,
                            CategoryID = c.CategoryID,
                            CategoryName = c.CategoryName,
                            lst_album = _album.getAllAlbum_bySingerID(s.SingerID).ToList(),
                            lst_song = _song.get10Song_bySingerid(s.SingerID).ToList(),
                            lst_video = _video.get10Video_bySingerid(s.SingerID).ToList(),
                            lstsong_sameType = _song.getRandom10Song_byCategoryID(Convert.ToInt32(s.CategoryID)).ToList(),
                            lstcomment_songID = _comment.getAllCommentBy_SongID(start,end, songid)
                        };
                return q.SingleOrDefault();
            }
            catch (FaultException e)
            {
                throw e;
            }
        }
        public ListenPage loadListenPage_noAlbum(int start, int end, int songid)
        {          
            try
            {
                var q = from s in _song.getSong_bySongID(songid)              
                        join m in _musician.getAllMusician() on s.MusicianID equals m.MusicianID
                        join c in _category.getAllCategory() on s.CategoryID equals c.CategoryID
                        select new ListenPage()
                        {
                            SongID = songid,
                            SingerID = s.SingerID,
                            SingerName = s.SingerName,
                            SongName = s.SongName,
                            Lyrics = s.Lyrics,
                            SongURL = s.SongURL,                         
                            MusicianID = m.MusicianID,
                            MusicianName = m.MusicianName,
                            CategoryID = c.CategoryID,
                            CategoryName = c.CategoryName,
                            lst_album = _album.getAllAlbum_bySingerID(s.SingerID).ToList(),
                            lst_song = _song.get10Song_bySingerid(s.SingerID).ToList(),
                            lst_video = _video.get10Video_bySingerid(s.SingerID).ToList(),
                            lstsong_sameType = _song.getRandom10Song_byCategoryID(Convert.ToInt32(s.CategoryID)).ToList(),
                            lstcomment_songID = _comment.getAllCommentBy_SongID(start, end, songid)
                        };
                return q.SingleOrDefault();
            }
            catch (FaultException e)
            {
                throw e;
            }
        }
        public ViewVideoPage loadVideoPage(int start, int end, int videoid)
        {
            try
            {
                var q = from s in _song.getSong_bySongID(videoid)
                        join m in _musician.getAllMusician() on s.MusicianID equals m.MusicianID
                        join c in _category.getAllCategory() on s.CategoryID equals c.CategoryID
                        select new ViewVideoPage()
                        {
                            SongID = videoid,
                            SingerID = s.SingerID,
                            SingerName = s.SingerName,
                            SongName = s.SongName,
                            Lyrics = s.Lyrics,
                            SongURL = s.SongURL,
                            MusicianID = m.MusicianID,
                            MusicianName = m.MusicianName,
                            CategoryID = c.CategoryID,
                            CategoryName = c.CategoryName,
                            lst_album = _album.getAllAlbum_bySingerID(s.SingerID).ToList(),
                            lst_video = _video.get10Video_bySingerid(s.SingerID).ToList(),
                            lst_video_sameType = _video.getRandom10Video_byCategoryID(Convert.ToInt32(s.CategoryID),Convert.ToInt32(s.TypeID)).ToList(),
                            lstcomment_songID = _comment.getAllCommentBy_SongID(start, end, videoid)
                        };
                return q.SingleOrDefault();
            }
            catch (FaultException e)
            {
                throw e;
            }
        }
        public double durationMP3(string url)
        {
            try {               
                WindowsMediaPlayer wmp = new WindowsMediaPlayer();
                IWMPMedia mediainfo = wmp.newMedia(url);
                return mediainfo.duration;
            }
            catch (FaultException e) { throw e; }
        }
    }
}